package com.example.yibu.modelConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class ExecuteThread implements AsyncConfigurer {
    @Bean("serviceTask")
    public ThreadPoolTaskExecutor serviceTask() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);  //设置核心线程数
        executor.setMaxPoolSize(10);   //设置最大线程数
        executor.setQueueCapacity(50);  //设置线程池的工作队列容量
        executor.setKeepAliveSeconds(60);  //设置非核心线程的空闲存活时间(超出核心线程数的线程)
        executor.setThreadNamePrefix("service-b");  //设置线程名称前缀
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());  //设置拒绝策略
        return executor;
    }

}
